﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlimDX;
using SlimDX.Direct3D9;

namespace Gype.UI.D3D9
{
    internal class SolidColorBrush : Brush
    {
        public SolidColorBrush(Rendertarget9 target, Color4 color)
        {
            mTextureBrush = new Texture(target.GraphicsDevice, 1, 1, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
            uint colorValue = 0;
            colorValue |= ((uint)(color.Alpha * 255.0f)) << 24;
            colorValue |= ((uint)(color.Red * 255.0f)) << 16;
            colorValue |= ((uint)(color.Green * 255.0f)) << 8;
            colorValue |= ((uint)(color.Blue * 255.0f)) << 0;

            Surface surf = mTextureBrush.GetSurfaceLevel(0);
            Surface.FromMemory(surf, BitConverter.GetBytes(colorValue), Filter.Box, 0, Format.A8R8G8B8, 4, new System.Drawing.Rectangle(0, 0, 1, 1));
            surf.Dispose();
        }

        internal override void ApplyToShader<T>(ShaderCompiler<T> shader, ConstantDescription description)
        {
            shader.SetValue(description, mTextureBrush);
        }

        private Texture mTextureBrush;
    }
}
